#! /usr/bin/env python
# -*- coding: utf8 -*-

# Alexandre Coster <acoster@rhrk.uni-kl.de>
# AG DBIS - Technische Universität Kaiserslautern

# mXML: Messy XML Generator
# This application generates a dirty (messy) XML file (with changes
# both on the values and the structure) based on a "clean" input

import zope.interface
import random
import sys

import base

class subs(object):
    zope.interface.implements(base.IAlgorithm)
    name = "subs"

    def __init__(self, node):
        self.__chars = ""
        self.__params = base.node_to_dictionary(node)

        if "charList" in self.__params.keys():
            self.__chars = node.find("./parameters/charList").text
        else:
            if self.__params["useUpper"].lower() == "true":
                self.__chars = self.__chars + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

            if self.__params["useLower"].lower() == "true":
                self.__chars = self.__chars + "abcdefghijklmnopqrstuvwxyz"

            if self.__params["useDigit"].lower() != "true":
                self.__chars = self.__chars + "0123456789"

            if self.__params["useSpace"].lower() != "true":
                self.__chars = self.__chars + " "

        self.__begin = 0
        if self.__params["includeFirst"].lower() == "false":
            self.__begin = 1

        self.__loffset = 0
        if self.__params["includeLast"].lower() == "false":
            self.__loffset = 1

    def __call__(self, string):
        if len(self.__chars) == 0:
            return string

        c = len(string)

        toAdd = random.choice(self.__chars)
        place = random.randint(self.__begin, c - self.__loffset)
        string = string[:place] + toAdd + string[place + 1:]

        return string

__plugins__ = (subs, )
